var webpack = require("webpack");

var HtmlWebpackPlugin = require('html-webpack-plugin');
//webpack可以将几个js的公共模块提取成一个chunk
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");

var path = require("path");
var srcDir = path.resolve(__dirname, '.');
var nodeModPath = path.resolve(__dirname, './node_modules');

module.exports = {
    resolve: {
    	root: [srcDir,nodeModPath],
        modulesDirectories: ['.'],		//告诉webpack所有模块的启始目录由当前目录开始
        extensions: ['.js',"",".css"],	//定义一些常用的文件拓展名，被定义了的文件格式，在引用的时候可以不加扩展名
        alias: {
        	avalon2: 'resources/js/avalon/avalon-2.0-debug.js',
            ztree:'resources/js/zTree_v3-3.5.24'
        }
    },
    externals: {
        jquery: 'window.$'	
    },
    /***entry 入口文件 让webpack用哪个文件作为项目的入口*/
    entry:{
    	vendor: ['ztree/js/jquery.ztree.core-3.5.js', 'avalon2'],
        index: "resources/template/portal/index",
        workspace: "resources/template/portal/workspace"
    },
    /**output 出口 让webpack把处理完成的文件放在哪里*/
    output: {
        path: path.join(__dirname, "assets"), //打包输出目录,
        publicPath: "/assets",     //webpack-dev-server访问的路径,
        filename: "[name].js",            //输出文件名
        chunkFilename: "bundle-[id].js"   //输出chunk文件名
    },
    /**module 模块 要用什么不同的模块来处理各种类型的文件*/
    module: {
        loaders:[{
                test: /\.css$/,		//把源文件迁移到指定的目录
                loader:'file?name=all.css'//'file?name='+__dirname+'[path]/[name].[ext]'	
            },{
            	test: /\.html$/, 
            	loader: 'raw!html-minify'
            },{
            	test: /index\.js$/, //暴露模块到全局window下
            	include:path.join(__dirname, "resources/template/portal"),
            	loader: 'expose?Index'
            },{
            	test: /workspace\.js$/, //暴露模块到全局window下
            	include:path.join(__dirname, "resources/template/portal"),
            	loader: 'expose?Workspace'
            }
        ]
    },
    plugins: [
        new CommonsChunkPlugin({
        	minChunks: 2,
            name: "common.chunk",
            chunks: ["index", "workspace"]
        }),
        new HtmlWebpackPlugin({
            filename: 'webpack.html',
        })
    ],
    'html-minify-loader': {	//处理avalon加载html模版，from司徒正美
        empty: true, // KEEP empty attributes
        cdata: true, // KEEP CDATA from scripts
        comments: true, // KEEP comments
        dom: {// options of !(htmlparser2)[https://github.com/fb55/htmlparser2]
            lowerCaseAttributeNames: false, // do not call .toLowerCase for each attribute name (Angular2 use camelCase attributes)
        }
    }
};


/**
npm install webpack
sudo npm install webpack -g #安装一个全局的命令

npm install raw-loader --save-dev
npm install expose-loader --save-dev
npm install file-loader --save-dev
npm install css-loader --save-dev
npm install style-loader --save-dev
npm install url-loader --save-dev
npm install html-minify-loader --save-dev
npm install image-webpack-loader --save-dev

npm install html-webpack-plugin --save-dev
npm install extract-text-webpack-plugin --save-dev

npm install glob --save-dev
npm install webpack-dev-server --save-dev
sudo npm install webpack-dev-server -g
*/

/*
//引入glob
var glob = require('glob')
//entries函数 自动构建入口函数
var entries= function () {
    var jsDir = path.resolve(srcDir, './resources/template/portal')
    var entryFiles = glob.sync(jsDir + '/*.js')
    var map = {};

    for (var i = 0; i < entryFiles.length; i++) {
        var filePath = entryFiles[i];
        var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'));
        map[filename] = 'resources/template/portal/'+filename;
    }
    return map;
}*/